package repository

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntityManager"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"gitee.com/fierce_wolf/go-fox-edge-common/edgeEntity"
)

type repoLocalScriptService struct {
}

func (e *repoLocalScriptService) ExtendCompOperateInfo(compEntityList []commEntity.IEntity) []map[string]interface{} {
	// 获得OperateEntity的Map
	operateEntityMap := e.getOperateEntityMap()

	// 将operateEntity的数量，添加到返回的内容之中
	mapList := make([]map[string]interface{}, 0)
	for _, entity := range compEntityList {
		compEntity := entity.(*edgeEntity.RepoCompEntity)

		data := commEntity.BuildMap(compEntity)

		compParam := Map.GetMap(data, "compParam", make(map[string]interface{}))

		manufacturer := Map.GetString(compEntity.CompParam, "manufacturer", "")
		deviceType := Map.GetString(compEntity.CompParam, "deviceType", "")
		list := operateEntityMap[manufacturer+"|"+deviceType]
		if list == nil {
			compParam["operateCount"] = 0
		} else {
			compParam["operateCount"] = len(list)
		}

		mapList = append(mapList, data)
	}

	// 根据ID排序
	commEntity.SortMapList(mapList)

	return mapList
}

func (e *repoLocalScriptService) getOperateEntityMap() map[string][]*edgeEntity.OperateEntity {
	result := make(map[string][]*edgeEntity.OperateEntity)

	commEntityManager.FindEntityList(edgeEntity.Type.OperateEntity.Type(), func(value commEntity.IEntity) bool {
		operateEntity := value.(*edgeEntity.OperateEntity)

		if operateEntity.EngineType != "JavaScript" {
			return false
		}

		key := operateEntity.Manufacturer + "|" + operateEntity.DeviceType

		operateEntities, ok := result[key]
		if !ok {
			operateEntities = make([]*edgeEntity.OperateEntity, 0)
		}
		operateEntities = append(operateEntities, operateEntity)

		result[key] = operateEntities
		return true
	})

	return result
}

func (e *repoLocalScriptService) getCompEntity(manufacturer, deviceType string) *edgeEntity.RepoCompEntity {
	compEntity := commEntityManager.FindEntity(edgeEntity.Type.RepoCompEntity.Type(), func(value commEntity.IEntity) bool {
		compEntity := value.(*edgeEntity.RepoCompEntity)

		if compEntity.CompType != "jsp-decoder" {
			return false
		}
		if manufacturer != compEntity.CompParam["manufacturer"] {
			return false
		}
		if deviceType != compEntity.CompParam["deviceType"] {
			return false
		}

		return true
	})

	if compEntity == nil {
		return nil
	}

	return compEntity.(*edgeEntity.RepoCompEntity)
}

func (e *repoLocalScriptService) getOperateEntityList(manufacturer, deviceType string) []*edgeEntity.OperateEntity {
	entityList := commEntityManager.FindEntityList(edgeEntity.Type.OperateEntity.Type(), func(value commEntity.IEntity) bool {
		entity := value.(*edgeEntity.OperateEntity)

		if entity.EngineType != "JavaScript" {
			return false
		}
		if manufacturer != entity.Manufacturer {
			return false
		}
		if deviceType != entity.DeviceType {
			return false
		}

		return true
	})

	resultList := make([]*edgeEntity.OperateEntity, 0)
	for _, entity := range entityList {
		resultList = append(resultList, entity.(*edgeEntity.OperateEntity))
	}

	return resultList
}
